/*
 * Copyright (c) 2021-2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifdef RECORDER_SUPPORT
#define LOG_TAG "main"

#include <iostream>
#include <memory>
#include <thread>
#include <chrono>
#include "audio_capture_creator.h"

#include "i_engine_factory.h"

extern "C" {
__attribute__((visibility("default"))) OHOS::Media::IEngineFactory* CreateEngineFactory();
}

using namespace OHOS::Media;

class RecorderEngineObs : public IRecorderEngineObs {
public:
    ~RecorderEngineObs() override = default;

    void OnError(ErrorType errorType, int32_t errorCode) override
    {
        std::cout<< "error"<< errorCode <<std::endl;
    }

    void OnInfo(InfoType type, int32_t extra) override
    {

    }
};

int main(int argc, char* argv[])
{
    std::string pcmPath = RESOURCE_DIR "/PCM/96000_2_03.pcm";
    OHOS::AudioStandard::AudioCaptureCreator::GetInstance().SetPcmPath(pcmPath);
    std::cout << "Use media_standard interface recorder." << std::endl;
    auto engineFactory = std::unique_ptr<OHOS::Media::IEngineFactory>(CreateEngineFactory());
    auto recorder = engineFactory->CreateRecorderEngine(0, 0, 0);
    auto obs = std::make_shared<RecorderEngineObs>();
    recorder->SetObs(obs);
    int32_t audioSourceId= 0;
    recorder->SetAudioSource(AudioSourceType::AUDIO_MIC,audioSourceId);
#ifdef VIDEO_SUPPORT
    int32_t videoSourceId= 0;
    recorder->SetVideoSource(VideoSourceType::VIDEO_SOURCE_SURFACE_YUV,videoSourceId);
    recorder->SetOutputFormat(OutputFormatType::FORMAT_MPEG_4);
#else
    recorder->SetOutputFormat(OutputFormatType::FORMAT_M4A);
#endif
    auto audSampleRate = AudSampleRate{96000};
    auto audChannel = AudChannel{2};
    auto audBitRate = AudBitRate{320000};
    auto auEncoder = AudEnc{AudioCodecFormat::AAC_LC};

    recorder->Configure(audioSourceId, audSampleRate);
    recorder->Configure(audioSourceId, audChannel);
    recorder->Configure(audioSourceId, audBitRate);
    recorder->Configure(audioSourceId, auEncoder);
#ifdef VIDEO_SUPPORT
    auto viRectangle = VidRectangle{1280,720};
    auto viBitRate = VidBitRate{12004000}; // bps
    auto viFrameRate = VidFrameRate{60};
    auto viCaptureRate = CaptureRate{60.0};
    auto viEncoder = VidEnc{VideoCodecFormat::H264};

    recorder->Configure(videoSourceId, viRectangle);
    recorder->Configure(videoSourceId, viBitRate);
    recorder->Configure(videoSourceId, viFrameRate);
    recorder->Configure(videoSourceId, viCaptureRate);
    recorder->Configure(videoSourceId, viEncoder);
#endif
    auto outFilePath = OutFilePath{"d:/Audio"};
    recorder->Configure(DUMMY_SOURCE_ID, outFilePath);
    auto ret = recorder->Prepare();
    if (ret != 0) {
        std::cout << "prepare error " << ret << std::endl;
    }
    recorder->Start();
#if 0
    std::cin.get();
    recorder->Stop();
    recorder->Pause();
    recorder->Resume();
#else
    std::this_thread::sleep_for(std::chrono::seconds(20));
    recorder->Stop();
    std::cout << "recorder demo exit" << std::endl;
#endif
    return 0;
}
#endif //RECORDER_SUPPORT
